Kwik.Page.include("Kwik.Ui.Animations.Base.js");
Kwik.Page.include("Kwik.Ui.Css.js");

/** 
* Animated movement of an element. When applied to a absolute positioned element it will use the style.top and style.left 
* properties, otherwise it will assume moving around another element en uses the style.marginLeft and style.marginTop properties.
* @constructor 
* @base Kwik.Ui.Animations.Base
* @requires Kwik.Ui.Animations.Base
* @created 1:58 AM 6/28/2009
* @modified 1:58 AM 6/28/2009
*/
Kwik.Ui.Animations.Move = function(config)
{
	/** @ignore */
	Kwik.Utility.extend(this, "Kwik.Ui.Animations.Base");
	this.__class.push("Kwik.Ui.Animation.Move");

	/** Starting position @type Object */
	this.begin = null;
	/** End position @type Object */
	this.end = null;
	/** Current position @type Object */
	this.active = {x:0, y:0};

	/** Sets the element and checks whether the current style is 'relative' or 'absolute' and sets it to 'relative' of neither */
	this.setElement = function Move_setElement(element)
	{
		this.Kwik$Ui$Animations$Base.setElement(element);
		var pos = Kwik.Ui.Css.getCurrentStyle(element, "position");
		if(pos != "absolute" && pos != "relative"){element.style.position = "relative";}
		return element;
	};

	/** Set the current step or this move animation, calculates the top left point by status, begin and end point */
	this.setCurrent = function Move_setCurent(step)
	{
		this.Kwik$Ui$Animations$Base.setCurrent(step);
		if(!this.end){return;}

		var pointNew = {x:0, y:0};
		pointNew.x = this.begin.x + Math.round((this.end.x - this.begin.x) / 100 * this.status);
		pointNew.y = this.begin.y + Math.round((this.end.y - this.begin.y) / 100 * this.status);

		if(pointNew.x != this.active.x || pointNew.y != this.active.y)
		{
			this.active.x = pointNew.x;
			this.active.y = pointNew.y;

			this.element.style.cssText += ";" + [
				"left:" + this.active.x.toString() + "px",
				"top:" + this.active.y.toString() + "px"
			].join(";");
		}
	};

	/** Configure the move animation */
	this.configure = function Move_configure(config)
	{
		config = this.Kwik$Ui$Animations$Base.configure(config);
		Kwik.Page.log.write("Kwik.Ui.Animations.Move.configure: Configuring move animation.");
		if(config.begin){this.begin = config.begin;}
		if(config.end){this.end = config.end;}
		if(Kwik.Type.isElement(config.element))
		{
			Kwik.Page.log.write("Kwik.Ui.Animations.Move.configure: Configuring begin with element.");
			if(this.begin == null)
			{
				var rect = Kwik.Ui.Utility.getRectangle(config.element);
				this.begin = {x: rect.x, y: rect.y};
				Kwik.Page.log.write("Kwik.Ui.Animations.Move.configure: Getting end from element dimensions (" + rect.toString() + ").");	
			}
		}
		if(config.status){this.setStatus(config.status);}
		if(config.speed){this.setSpeed(config.speed);}
	};

	this.setSpeed = function Move_setSpeed(pixelsPerSecond)
	{

		var a = this.begin.x - this.end.x;
		var b = this.begin.y - this.end.y;
		a = a<0? -a: a;
		b = b<0? -b: b;
		a = a*a;
		b = b*b;
		var totalPixels = Math.sqrt(a+b);
		this.duration = Math.round((totalPixels / pixelsPerSecond) * 1000);

		Kwik.Page.log.write("Kwik.Ui.Animations.Move.setSpeed: Speed:" + pixelsPerSecond.toString() + ", Duration:" + this.duration.toString());
	};

	/** Get the properties as string @type String */
	this.toString = function Move_toString()
	{
		return [
			this.Kwik$Ui$Animations$Base.toString(),
			"x=" + this.active.x.toString(),
			"y=" + this.active.y.toString()
			].join(", ");
	}

	/** @ignore */
	this.main = function(config)
	{
		if(config){this.configure(config);}
	}; this.main(config);
};
